package com.lsu.app.controller.Student;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsu.app.bean.Course;
import com.lsu.app.bean.KvCode;
import com.lsu.app.bean.TreeData;
import com.lsu.app.config.target.RequestRoles;
import com.lsu.app.service.*;
import com.lsu.app.tool.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author hjj
 * @date 2023/11/15 16:39
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/tree")
public class treeController {
    private final KvCodeService kvCodeService;
    private final ResourceService resourceService;
    private final CommentService commentService;
    private final PraiseService praiseService;
    private final CollectService collectService;
    private final CourseService courseService;
    private final KindService kindService;

    public treeController(KvCodeService kvCodeService, ResourceService resourceService, CommentService commentService, PraiseService praiseService, CollectService collectService, CourseService courseService, KindService kindService) {
        this.kvCodeService = kvCodeService;
        this.resourceService = resourceService;
        this.commentService = commentService;
        this.praiseService = praiseService;
        this.collectService = collectService;
        this.courseService = courseService;
        this.kindService = kindService;
    }

    @RequestRoles({"student"})
    @GetMapping("/get")
    @ApiOperation("获取树数据")
    public R<List<TreeData>> get(){
        List<KvCode> ks = kvCodeService.getOneKinds();
        List<TreeData> trees = new ArrayList<>();
        for (KvCode k : ks) {
            TreeData tree = new TreeData(k.getName(),kvCodeService.getTwoKindsName(k.getValue()).toArray(new String[0]));
            trees.add(tree);
        }
        return R.success(trees);
    }

    @RequestRoles({"student"})
    @GetMapping("/getSet")
    @ApiOperation("根据树数据获取推荐课程集合")
    public R<Page<Course>> getSets(
        @ApiParam("当前页")@RequestParam("pageCur") Integer pageCur,
        @ApiParam("页码")@RequestParam("pageSize") Integer pageSize,
        @ApiParam("树类型名称")@RequestParam("search") String search,
        @ApiParam("课程名称")@RequestParam(value = "courseName",required = false) String courseName
    ){
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        Page<Course> page;
        if(search!=null&&!search.equals("")){
            //判断是否为一级分类
            if(kvCodeService.isOneKind(search)){
                List<String> str = kvCodeService.getKindNamesByName(search);
                str.add(search);
                Set<Integer> sets = new HashSet<>();
                for (String s : str) {
                    sets.addAll(kindService.getCourseIdsByKindName(s));
                }
                wrapper.in(Course::getId,sets);
            }else{
                List<Integer> courseIdsByKindName = kindService.getCourseIdsByKindName(search);
                if (courseIdsByKindName.size()!=0){
                    wrapper.in(Course::getId,courseIdsByKindName);
                }else{
                    wrapper.in(Course::getId,0);
                }
            }
            wrapper.orderByDesc(Course::getClickNum);
            page = courseService.page(new Page<>(pageCur,pageSize),wrapper);
        }else{
            if(courseName!=null&&!courseName.equals("")){
                page=courseService.page(new Page<>(pageCur,pageSize),new LambdaQueryWrapper<Course>().eq(Course::getCourseName,courseName));
            }else{
                return R.err("课程名称错误");
            }
        }
        for (Course course : page.getRecords()) {
            course.setTeacherName(courseService.getTeacherNameByTeacherId(course.getTeacherId()));
            course.setTeacherImgUrl(resourceService.getHeadUrlByUorrIdAndType(course.getTeacherId()));
            course.setCoverImgUrl(resourceService.getCoverImgUrlByCourseId(course.getId()));
            course.setResponds(commentService.getCountByTypeAndLinkId(1,course.getId()));
            course.setKindName(kindService.getValueByCourseId(course.getId()));
            course.setSelfLike(praiseService.getSelfLike(course.getId(),3));
            course.setSelfCollect(collectService.collectCount(course.getId(),2));
        }
        return R.success(page);
    }


}
